package cn.iocoder.yudao.module.enterprise.controller.admin.reductionscene;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.enterprise.controller.admin.reductionscene.vo.ReductionSceneCreateReqVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.reductionscene.vo.ReductionScenePageReqVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.reductionscene.vo.ReductionSceneRespVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.reductionscene.vo.ReductionSceneUpdateReqVO;
import cn.iocoder.yudao.module.enterprise.convert.reductionscene.ReductionSceneConvert;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.reductionscene.ReductionSceneDO;
import cn.iocoder.yudao.module.enterprise.service.reductionscene.ReductionSceneService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name =  "管理后台 - 减排场景")
@RestController
@RequestMapping("/enterprise/reduction-scene")
@Validated
public class ReductionSceneController {

    @Resource
    private ReductionSceneService reductionSceneService;

    @PostMapping("/create")
    @Operation(summary = "创建减排场景")
    @PreAuthorize("@ss.hasPermission('enterprise:reduction-scene:create')")
    public CommonResult<Long> createReductionScene(@Valid @RequestBody ReductionSceneCreateReqVO createReqVO) {
        return success(reductionSceneService.createReductionScene(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新减排场景")
    @PreAuthorize("@ss.hasPermission('enterprise:reduction-scene:update')")
    public CommonResult<Boolean> updateReductionScene(@Valid @RequestBody ReductionSceneUpdateReqVO updateReqVO) {
        reductionSceneService.updateReductionScene(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除减排场景")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('enterprise:reduction-scene:delete')")
    public CommonResult<Boolean> deleteReductionScene(@RequestParam("id") Long id) {
        reductionSceneService.deleteReductionScene(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得减排场景")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('enterprise:reduction-scene:query')")
    public CommonResult<ReductionSceneRespVO> getReductionScene(@RequestParam("id") Long id) {
        ReductionSceneDO reductionScene = reductionSceneService.getReductionScene(id);
        return success(ReductionSceneConvert.INSTANCE.convert(reductionScene));
    }

    @GetMapping("/page")
    @Operation(summary = "获得减排场景分页")
    @PreAuthorize("@ss.hasPermission('enterprise:reduction-scene:query')")
    public CommonResult<PageResult<ReductionSceneRespVO>> getReductionScenePage(@Valid ReductionScenePageReqVO pageVO) {
        PageResult<ReductionSceneDO> pageResult = reductionSceneService.getReductionScenePage(pageVO);
        return success(ReductionSceneConvert.INSTANCE.convertPage(pageResult));
    }

}
